package com.WhatsUp.tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.WhatsUp.User.NowUser;

public class Attendance {
	private static Attendance Singleton = null;
	private static boolean isrunning = false;

	private Attendance() {
		
	}

	public static synchronized Attendance getInstant() {
		if (Singleton == null) {
			Singleton = new Attendance();
		}
		if(isrunning == false){
			Singleton.run();
		}
		return Singleton;
	}

	private Timer timer = new Timer("Attendance", false);

	private void run() {
		isrunning = true;
		timer.scheduleAtFixedRate(new TimerTask() {
			ServerHelper server = ServerHelper.getInstant();

			@Override
			public void run() {
				if (server.IsConnected() && NowUser.getInstant().isLogined()) {
					Map<String, String> vars = new HashMap<String, String>();
					vars.put("name", "USERID");
					String result = null;
					try {
						result = server.sendRequest("login.php", vars);
					} catch (IOException e) {
						android.util.Log.i("TEST", "Attendance Failed");
					}
					android.util.Log.i("TEST", "Attendance success");
				}
			}
		}, 100, 60000);
	}

	public void cancel() {
		timer.cancel();
		isrunning = false;
	}
	
	public boolean isRunning(){
		return isrunning;
	}
}
